package org.jeecg.modules.dlxj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.SneakyThrows;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.dlxj.entity.DlxjDebugStatistics;
import org.jeecg.modules.dlxj.mapper.DlxjDebugStatisticsMapper;
import org.jeecg.modules.dlxj.service.IDlxjDebugStatisticsService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 消缺记录统计表
 * @Author: lzl
 * @Date:   2022-07-29
 * @Version: V1.0
 */
@Service
public class DlxjDebugStatisticsServiceImpl extends ServiceImpl<DlxjDebugStatisticsMapper, DlxjDebugStatistics> implements IDlxjDebugStatisticsService {

    @Autowired
    private DlxjDebugStatisticsMapper dlxjDebugStatisticsMapper;

    @Value("${jeecg.path.upload}")
    private String upLoadPath;

    /**
     * 消缺统计列表
     * @return
     */
    @Override
    public List<DlxjDebugStatistics> debugStatisticsList() {
        return dlxjDebugStatisticsMapper.debugStatisticsList();
    }

    /**
     * 消缺统计，通过任务id查询
     * @param taskId
     * @return
     */
    @Override
    public List<DlxjDebugStatistics> queryById(String taskId) {
        return dlxjDebugStatisticsMapper.queryById(taskId);
    }

    /**
     * 导出全部数据
     * @param request
     * @param dlxjDebugStatistics
     * @param clazz
     * @param title
     * @param taskId
     * @return
     */
    @SneakyThrows
    @Override
    public ModelAndView exportExcel(HttpServletRequest request, DlxjDebugStatistics dlxjDebugStatistics, Class<DlxjDebugStatistics> clazz, String title,String taskId) {
        // Step.1 组装查询条件
        //QueryWrapper<DlxjDebugStatistics> queryWrapper = QueryGenerator.initQueryWrapper(dlxjDebugStatistics, request.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        // Step.2 获取导出数据
        List<DlxjDebugStatistics> pageList;
        if (taskId!=null){
            pageList = dlxjDebugStatisticsMapper.queryById(taskId);}
        else{
            pageList = dlxjDebugStatisticsMapper.debugStatisticsList();
        }

        List<DlxjDebugStatistics> exportList = null;

        //过滤选中数据
        String selections = request.getParameter("selections");
        if (oConvertUtils.isNotEmpty(selections)) {
            List<String> selectionList = Arrays.asList(selections.split(","));
            exportList = pageList.stream().filter(item -> selectionList.contains(item.getPoleId())).collect(Collectors.toList());
        } else {
            exportList = pageList;
        }

        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.FILE_NAME, title);
        mv.addObject(NormalExcelConstants.CLASS, clazz);
        //update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
        ExportParams exportParams=new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title);
        exportParams.setImageBasePath(upLoadPath);
        //update-end--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置----------------------
        mv.addObject(NormalExcelConstants.PARAMS,exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;

    }
}
